import wx
import wx.grid as  gridlib
import rememberfloatwnd
import picbtn
import win32api
import win32con
import win32gui
import dbhelper
class RemmemberGrid(gridlib.Grid):
    def __init__(self, *args, **kwds):
        gridlib.Grid.__init__(self, *args, **kwds)
        self.CreateGrid(0,1)
        self.SetMargins(0,0)
        self.EnableGridLines(False)
        self.SetRowLabelSize(0)
        self.SetColLabelSize(0)
        self.EnableEditing(False)
        self.EnableDragGridSize(False)
        self.EnableDragColMove(False)
        self.HideCellEditControl()
        self.AutoSizeColumns(False)
        self.SetSelectionMode(gridlib.Grid.SelectRows)
        self.SetDefaultCellBackgroundColour(wx.Colour(239, 247, 255))
        self.SetSelectionBackground(wx.Colour(181, 215, 247))
        self.SetSelectionForeground(wx.BLACK)
        self.EnableScrolling(False ,False)
        self.Bind(wx.EVT_SIZE, self.OnSize)
    def OnSize(self, event):
        self.SetColSize(0, event.GetSize()[0]-15)
        self.Refresh()
        event.Skip()
        
class RememberListWnd(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = (~(wx.RESIZE_BORDER)) &(~wx.VSCROLL) &( wx.FULL_REPAINT_ON_RESIZE | wx.SIMPLE_BORDER |wx.FRAME_NO_TASKBAR| wx.STAY_ON_TOP)
        self.GridWidth = kwds["size"][0] -10
        self.GridNotesData = {}
        wx.Frame.__init__(self, *args, **kwds)
        #self.SetBackgroundColour(wx.Colour(181, 219, 247))      
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        
        #self._SetWndTransparent()
        self._CreateSubWidget()
    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.GradientFillLinear(self.GetClientRect(),wx.Colour(181, 219, 247),wx.Colour(255,255,255), wx.NORTH)
    
    def _SetWndTransparent(self):
        hwnd = self.GetHandle() 
        _winlib = win32api.LoadLibrary("user32") 
        pSetLayeredWindowAttributes = win32api.GetProcAddress(_winlib,"SetLayeredWindowAttributes") 
        exstyle = win32api.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) 
        if 0 == (exstyle & 0x80000):
            win32api.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, exstyle | 0x80000)          
            win32gui.SetLayeredWindowAttributes(hwnd, 0, 200, 2) #set the transparent style
    def _CreateSubWidget(self):
        self.vMainSizer = wx.BoxSizer(wx.VERTICAL)
        
        #self.CloseBtn = picbtn.PicBtn(self,-1,"D:/test/x2.bmp","D:/test/x1.bmp",(15,15),"test tip")
        #self.CloseBtn.Bind(wx.EVT_BUTTON, self.OnClose, self.CloseBtn)
                
        #self.vMainSizer.Add(self.CloseBtn, 0, wx.ALIGN_RIGHT|wx.RIGHT, 4)
        self.vMainSizer.Add(self._CreateGrid(), 1, wx.EXPAND|wx.RIGHT|wx.LEFT|wx.BOTTOM, 4)
        
        self.SetSizer(self.vMainSizer)
        self.Layout()
    
    def _CreateGrid(self):
        self.grid = RemmemberGrid(self)
  
        
        #self.grid.SetColSize(0,self.GridWidth)
        
        
        self.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDbClick)
        return self.grid 
    
    def OnDbClick(self, event):
        noteID = self.GridNotesData["%d"%event.GetRow()].get("ID")
        if noteID == None: return
        floatWnd = rememberfloatwnd.RememberFloatWnd(self.GetParent(), -1, "", NoteID = int(noteID)) # must cast to int 
        self.GetParent().AddRememberFloatWndRef(floatWnd)
        floatWnd.Show()
        
    def OnClose(self, event):
        self.Close()
    def ReQuery(self, queryPara):
        self._ClearAll()
        self._QueryRemembers(queryPara)
    def _ClearAll(self):
        rowNum = self.grid.GetNumberRows()
        if rowNum != 0:
            self.grid.DeleteRows(0, rowNum)
    def _QueryRemembers(self, querypara):
        if "all" == querypara:
            querypara = ""
        db = dbhelper.DbHelper('test.db',new=False)
        Curr = 0
        self.GridNotesData.clear()
        for note in db.GetNotes(querypara):
            note_id = note.get('ID')
            note_tag = note.get('tag')
            note_content = note.get('content')
            self.grid.InsertRows(Curr)
            attrOdd = gridlib.GridCellAttr()
            attrOdd.SetBackgroundColour(wx.Colour(239,247,255))
            attrEven = gridlib.GridCellAttr()
            attrEven.SetBackgroundColour(wx.Colour(214,235,255))
                    
            if Curr % 2 == 0:
                self.grid.SetRowAttr(Curr, attrOdd)
            else:
                self.grid.SetRowAttr(Curr, attrEven)
                    
            self.grid.SetRowSize(Curr,50)
            self.grid.SetCellValue(Curr, 0, note_content)
            self.GridNotesData["%d"%Curr] = note
            Curr = Curr + 1
        self._AddBlankRow(Curr)
        db.Disconnect()
    def _AddBlankRow(self,rowNum):
        if rowNum < 7:
            BlankNum = 7 - rowNum
        else:
            BlankNum = 0
        for Curr in range(BlankNum):
            CurrRow = rowNum + Curr
            self.grid.InsertRows(CurrRow)
            attrOdd = gridlib.GridCellAttr()
            attrOdd.SetBackgroundColour(wx.Colour(239,247,255))
            attrEven = gridlib.GridCellAttr()
            attrEven.SetBackgroundColour(wx.Colour(214,235,255))
                    
            if CurrRow % 2 == 0:
                self.grid.SetRowAttr(CurrRow, attrOdd)
            else:
                self.grid.SetRowAttr(CurrRow, attrEven)
                    
            self.grid.SetRowSize(CurrRow,50)
            self.grid.SetCellValue(CurrRow, 0, " ")
    
    
    
if __name__ == "__main__":
    app = wx.PySimpleApp()
    main = RememberListWnd(None, -1, "Notes")
    main.Show()
    app.MainLoop()
